<?php
/**
 * Created by PhpStorm.
 * User: fanglong
 * Date: 2017/11/3
 * Time: 09:08
 */

namespace app\queue\model;

use think\Db;
use think\Model;
use think\facade\View;
use think\facade\Cache;
use think\facade\Queue;
use think\queue\Job;
use app\queue\model\Test;

 
use think\facade\Log;
class settlementStatistical extends model
{
    protected $resultSetType = 'array';

    /**
     * @var string
     */
    protected $table='vi_lbx_settlement_statistical';

    /**
    *   数据库时间戳自动格式化 如果不需要就设置成false
    */
    // protected $dateFormat = false;
    protected $dateFormat = 'Y-m-d';

    /**
     * @var array
     */
    protected $auto=[];
    // /**
    //  * @var array
    //  */
    // protected $insert=['create_time','update_time'];

    // /**
    //  * @return mixed
    //  */
    // protected function setCreateTimeAttr()
    // {
    //     return $_SERVER['REQUEST_TIME'];

    // }

    // /**
    //  * @return mixed
    //  */
    // protected function setUpdateTimeAttr()
    // {
    //     return $_SERVER['REQUEST_TIME'];
    // }


 
	/**
	 * 执行循环插入一条记录
	 * $start_time 开始时间
	 * $end_time结束时间
	 * $branch_union_id   branch表的unioniq
	 * $perform_time      结算月份
	 * $t_name  表名
	 */
	public function add_percentage($start_time,$end_time,$branch_union_id,$t_name,$perform_time){
		
		$over_num=($end_time-$start_time)/1200;// 计算要执行多少轮
		$arr = '';
		$arr = array(
	        'num'=>0,
	        'over_num'=>$over_num,
	        'percentage'=>0,
	        'branch_union_id'=>$branch_union_id,
	        't_name'=>$t_name,
	        'perform_time'=>$perform_time,
	        'create_time'=>time()
        );
		
		$percentage   = $this->where(array('t_name'=>$t_name,'branch_union_id'=>$branch_union_id,))->find();
        if(!empty($percentage)){
            $this->where(array('id'=>$percentage['id']))->update($arr);
        }else{
            $this->insert($arr);
        }
	}
	
	/**
	 * 执行循环插入一条记录
	 * $branch_union_id   branch表的unioniq
	 * $t_name  表名
	 */
	public function edit_percentage($branch_union_id,$t_name,$perform_time){
		$this->where(array('branch_union_id'=>$branch_union_id,'t_name'=>$t_name,'perform_time'=>$perform_time))->inc('num')->update();
        $info = $this->where(array('branch_union_id'=>$branch_union_id,'t_name'=>$t_name,'perform_time'=>$perform_time))->find();

		$percentage =  $info['num']/$info['over_num'];

        $percentage = ceil($percentage*100);

        if($percentage>=100){
            $percentage = 100;
        };
        
        $this->where(array('branch_union_id'=>$branch_union_id,'t_name'=>$t_name,'perform_time'=>$perform_time))->update(array('percentage'=>$percentage));
		
	}


}   
?>  